<?php
class adminController extends baseController{
	protected $appID = 'admin';

	public function __construct(){
		$session= new cpSessionCache(config('SESSION'));
		$session->run();
		$appID = config('appID');
		$this->appID = empty($appID) ? $this->appID : $appID;
		$this->appID = $this->appConfig['COOKIE_PREFIX'].$this->appID;
		$this->checkLogin();
		parent::__construct();
	}

	//模板显示含公共
	protected function show($tpl = ''){
		$content=$this->display($tpl, true, true);
		$layout=$this->display('common', true, true,'admin');
		echo str_replace('<!--common-->',$content,$layout);
	}

	protected function checkPurview(){
		$info=$this->getLogin();
		$user_group=api('admin_user','group_info',$info['gid']);
		if($user_group['keep']){
			return true;
		}
		$base_purview=unserialize($user_group['base_purview']);
		
		$purview_info=api(APP_NAME,'hook_purview_admin');
		if(empty($purview_info)){
			return true;
		}
		$controller=$purview_info[CONTROLLER_NAME];
		if(empty($controller['auth'])){
			return true;
		}
		$action=$controller['auth'][ACTION_NAME];
		if(empty($action)){
			return true;
		}

		$current=APP_NAME.'_'.CONTROLLER_NAME.'_'.ACTION_NAME;

		if(!in_array($current, (array)$base_purview)){
			$this->msg('您没有权限访问此功能！',0);
		}
		return true;
		

	}

	protected function checkLogin(){
		//不需要登录验证的页面
		$noLogin = array(
						'login'=>array('index','login'),
				);
		
		//如果当前访问是无需登录验证，则直接返回		
		if( isset($noLogin[CONTROLLER_NAME]) && in_array(ACTION_NAME, $noLogin[CONTROLLER_NAME]) ){
			return true;
		}
		
		//没有登录,则跳转到登录页面
		if(!$this->isLogin()){
			$this->redirect( url('login/login/index') );
		}
		$this->checkPurview();
		return true;
	}

	//获取登录
	protected function getLogin(){
		return $_SESSION[ $this->appID . '_uid' ];
	}
	
	//判断是否登录
	protected function isLogin(){
		$session=in($_GET['session']);
		if($session&&$session<>session_id()){
			session_destroy();
			session_id($session);
			@session_start();
		}
		$appconfig=config('APP');
		if($_REQUEST['SAFE_KEY']&&$_REQUEST['SAFE_KEY']==$appconfig['SAFE_KEY']){
			return true;
		}
		if( empty( $_SESSION[ $this->appID . '_uid' ] ) ){
			return false;
		}else{
			$this->userInfo = $_SESSION[ $this->appID . '_uid' ];
			return true;
		}
	}
	
	//设置登录
	protected function setLogin( $userInfo ){
		$_SESSION[ $this->appID . '_uid' ] = $userInfo;
	}
	
	//退出登录
	protected function clearLogin( $url='' ){
		$_SESSION[ $this->appID . '_uid' ] = NULL;
		if( !empty($url) ){
			$this->redirect( $url );
		}
		return true;
	}

	//判断空值
    public function alert_str($srt,$type=0,$json=false)
    {
        switch ($type) {
            case 'int':
                $srt=intval($srt);
                break;
            default:
                $srt=in($srt);
                break;
        }
        if(empty($srt)){
            if($json){
                $this->msg('内部通讯错误！',0);
            }else{
                $this->error('内部通讯错误！');
            }
        }
    }
}